Integer
An integer is a real number without fractional parts. The set of integers is \mathbb{Z} = {...-3,-2,-1,0,1,2,3...} . In computer science, integers are sometimes divided into two categories (data types): signed, which can store both negative and nonnegative integers, and unsigned, which can only store nonnegative integers. Representation of integers in computer memory The most common representation of an integer is a 32-bit (4-byte) integer. Other common types are short (16-bit, 2-byte), long (64-bit, 8-byte) and byte (8-bit, 1-byte). Some programming frameworks and languages introduce arbitrary-precision integers, which can store any number of digits. For unsigned integers, the bits are stored as if they were in binary in real life, considering the space for the data type. For example, for an unsigned byte: The range for 8 bits is 0 to 255, in general, the range is 0 to 2n-1 for n bits. Negative integers and values greater than 255 cannot be specified this way. To incorporate the sign with the representation, the binary system is extended to include it. ; Sign-and-magnitude Here, the first bit represents the sign. 0 is positive and 1 is negative. As a consequence, a negative zero ( 000 0000) exists. (Negative zero is a consequence in computer science that arises from using this kind of representation) The range of values for 8 bits is -127 ( 111 1111) to 127 ( 111 1111). In general, the range is -(2n/2-1) to 2n/2-1 for n bits. ; Ones' complement The ones' complement system can also be used to represent negative numbers. A negative integer is represented by doing a bitwise negation (complement, bitwise NOT) to its positive counterpart. For example, the bitwise negation of 0000 0010 (2) is 1111 1101, which corresponds to -2. Again, negative zero (1111 1111) exists. Addition in binary is also possible with an end-around carry. The range of values is -127 to 127 (-(2n/2-1) to 2n/2-1, in general). Consequently, negative integers start with a bit 1 and positive integers start with a bit 0. ; Two's complement The two's complement eliminated the need for an end-around carry system. The negative of a number is represented by the ones' complement (bitwise negation) of the number added by one. For example, to get the negative of 5 (0000 0101), do a ones' complement: 1111 1010, then add 1: 1111 1011 (-5). To get the negative of -5 (positive 5), do a ones' complement: 0000 0100, then add 1: 0000 0101 (5). Notice that we arrived at the same result. There is only one zero in this system. The range is from -128 to 127, in general, -(2n-1) to 2n/2-1. This is the system commonly used. ; Excess-''N'' Excess-''N'' uses a predefined number N as a biasing value. For example, to convert -9 (0000 1001) to Excess-127, add 127 to the number: -9 + 127 = 122, which corresponds to 0111 1010, -9's Excess-127 representation. ; Base -2 This system uses a "base" of -2 to represent signed integers. From the right, the first bit is 1, then -2, then 4, then -8, and so on. The range is not symmetric; if the number of bits is even, the magnitude of the largest negative number that can be represented is twice as large as the largest positive number that can be represented, and vice versa if the word has an odd number of bits. ; Binary-coded decimal Although this system is not that efficient, some machines still use this method. This system encodes each digit of a decimal (base-10) number into binary. Each digit is stored in 4 bits. For example, 109 in BCD is 0001 0000 1001. To represent signs, another group of 4 bits is added to the end of a BCD: 1101 for negative signed, 1101 for positive signed, and 1111 for unsigned. ; Gray code The reflected binary code, also known as Gray code, is a binary numeral system where two successive values differ in only one digit. Formal grammar The grammar for unsigned integers is: → → | → "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" → "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" The grammar for signed integers is: → + | - (may sometimes implicitly cover the option ) In general, the grammar for integers is: → | Converting to storage * Convert decimal (base-10) representation into a string. Using ASCII, each digit 0 to 10 would correspond to code points 30 to 39 (hex), respectively. * Directly store its binary representation. Category:Data types